<?php

!defined('IN_APP') && exit('Access Denied');

class usercontrol extends base {

    function __construct(& $get, & $post) {
        parent::__construct($get, $post);
        $this->load("user");
        $this->load("customer");
        $this->load("area");
    }

    /* 我的客户 */

    function onmycustomer() {
        $customerlist = $_ENV['customer']->get_by_operator($this->user['username']);
        include template('mycustomer');
    }

    /* 个人资料 */

    function onprofile() {
        $citylist = $_ENV['area']->get_by_pid(6);
        if (isset($this->post['submit'])) {
            $password = empty($this->post['password']) ? $this->user['password'] : md5($this->post['password']);
            $areaid = $this->post['areaid'];
            $positionid = $this->post['positionid'];
            $avatar = $this->user['avatar'];
            if ($_FILES['avatar']['tmp_name']) {
                $avatardir = "/data/avatar/";
                $extname = extname($_FILES["avatar"]["name"]);
                if (isimage($extname)) {
                    $upload_tmp_file = APP_ROOT . '/data/tmp/' . $this->user['uid'] . '.' . $extname;
                    $avatar = '/data/avatar/' . $this->user['uid'] . '.' . $extname;
                    move_uploaded_file($_FILES["avatar"]["tmp_name"], $upload_tmp_file);
                    image_resize($upload_tmp_file, APP_ROOT . $avatar, 61, 72);
                }
            }
            $_ENV['user']->update($password, $avatar, $positionid, $areaid);
            $this->user = $_ENV['user']->get($this->user['uid']);
        }
        include template("profile");
    }

    /* 今日跟单提醒 */

    function ondaily() {
        $todaystart = strtotime(date("Y-m-d 00:00:00"));
        $todayend = strtotime(date("Y-m-d 23:59:59"));
        $customerlist = $_ENV['customer']->get_by_status(2, $todaystart, $todayend);
        include template("mycustomer");
    }

    /* 费用收款提示 */

    function onfinalpay() {
        
    }

    function onlogin() {
        if ($this->user['uid'] !== 0)
            header("Location:" . SITE_URL);
        if (isset($this->post['submit'])) {
            $username = trim($this->post['username']);
            $password = md5($this->post['password']);
            $user = $_ENV['user']->get_by_username($username);
            if ($password == $user['password']) {
                $_ENV['user']->refresh($user['uid']);

                header("Location:" . SITE_URL);
            }
        }
        include template("login");
    }

    function onlogout() {
        $_ENV['user']->logout();
        header("Location:" . SITE_URL);
    }

    /* 个人业绩 */

    function onresult() {
        $statusresult = $_ENV['customer']->get_sum_status();
        include template("myresult");
    }

}

?>